package com.fyi.controller;

import com.fyi.entity.Article;
import com.fyi.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 跳转后台首页
     */
    @RequestMapping("backIndexView")
    public String backIndex(){
        return "backView/backIndex";
    }

    /**
     * 分页
     */



    /**
     * 查询所有文章页面
     * @return
     */
    @RequestMapping("ShowArticleView")
    public String ArticleManagerView(){
        return "backView/showArticle";
    }

    @ResponseBody
    @RequestMapping(value = "/articlePageList",produces = "application/json;charset=utf-8")
    public PageInfo<Article> selectAllPageArticle(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum, Model model) throws IOException {
        PageInfo<Article> page  = articleService.selectAllPageArticle(pageNum,3);
        model.addAttribute("pageInfo",page);
        return page;
    }

    @RequestMapping("/selectAllArticle")
    @ResponseBody
    public Map selectAllArtice(){
        List<Article> selectAllArticle = articleService.selectAllArticle();
        System.out.println(selectAllArticle);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectAllArticle);
        return result;
    }

    /**
     * 根据Id查询
     */
    @RequestMapping("/selectArticleById/{articleId}")
    @ResponseBody
    public Map selectArticeById(@PathVariable Integer articleId){
        Article selectById = articleService.selectById(articleId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectById);
        return result;
    }
    /**
     * 文章管理页面
     */
    @RequestMapping("ArticleManagerView")
    public String ArticleORMView(Model model){
        model.addAttribute("article",new Article());
        return "backView/articleManager";
    }
    /*添加文章*/
    @RequestMapping("addArticle")
    @ResponseBody
    public Map addArticle(@ModelAttribute Article article){

        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        String time=sdf.format(d);
        System.out.println("--"+time);
        article.setCreateTime(time);
        article.setUpdateTime(time);

        Integer addArticle = articleService.addArticle(article);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "添加成功");
        result.put("data", addArticle);
        return result;
    }
    /*修改文章*/

    @RequestMapping("updateArticle")
    @ResponseBody
    public Map updateArticle(@ModelAttribute Article article){

        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        String time=sdf.format(d);
        System.out.println("--"+time);
        article.setUpdateTime(time);

        Integer updateArticle = articleService.updateArticle(article);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "修改成功");
        result.put("data", updateArticle);
        return result;
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteArticle/{articleId}")
    @ResponseBody
    public Map deleteArtice(@PathVariable Integer articleId){
        Integer deleteArticle = articleService.deleteArticle(articleId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", deleteArticle);
        return result;
    }
}
